package com.ipzoe.app.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.ipzoe.app.dto.BannerResponseDTO;
import com.ipzoe.app.dto.CaseResponseDTO;
import com.ipzoe.app.dto.PageDTO;
import com.ipzoe.cms.domain.Case;
import com.ipzoe.cms.service.ICaseService;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.core.page.TableDataInfo;
import com.ipzoe.common.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.catalina.connector.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("api/v1/case")
@Api(tags = "资讯案例")
public class CaseController extends BaseController {

    @Autowired
    private ICaseService iCaseService;

    /**
     * 查询案例列表
     */
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("查询案例列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = CaseResponseDTO.class),
    })
    public TableDataInfo list(@RequestParam Request request)
    {
        startPage();
        List<Case> list = iCaseService.list(new QueryWrapper<Case>().eq(Case.COLUMN_USE_FLAG, 1)
        .orderByAsc(Case.COLUNM_ORDER_NUM));
        List<CaseResponseDTO> caseResponseDTOS = Lists.newArrayList();
        list.forEach(item -> {
            CaseResponseDTO caseResponseDTO = new CaseResponseDTO();
            BeanUtil.copyProperties(item, caseResponseDTO);
            if (caseResponseDTO.getUpdateTime() == null) {
                caseResponseDTO.setUpdateTime(item.getCreateTime());
            }
            caseResponseDTOS.add(caseResponseDTO);
        });
        return getDataTable(caseResponseDTOS);
    }

    /**
     * 查询案例详情
     */
    @PostMapping("/detail/{id}")
    @ResponseBody
    @ApiOperation("查询案例详情")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = CaseResponseDTO.class),
    })
    public AjaxResult detail(@PathVariable Long id)
    {
        if (id == null) {
            throw new CustomException(400, "id不能为空");
        }
        Case one = iCaseService.getOne(new QueryWrapper<Case>().eq("id", id));
        if (one == null) {
            throw new CustomException(400, "查无此案例资讯");
        }
        CaseResponseDTO caseResponseDTO = new CaseResponseDTO();
        BeanUtil.copyProperties(one, caseResponseDTO);
        if (caseResponseDTO.getUpdateTime() == null) {
            caseResponseDTO.setUpdateTime(one.getCreateTime());
        }
        return AjaxResult.success(caseResponseDTO);
    }
}
